package com.begye.smartloan.apptest.renzheng.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.DiaFillItemBangKaSameChannelBinding
import com.begye.smartloan.apptest.ext.isUrl
import com.begye.smartloan.apptest.tool.addTouch
import com.bumptech.glide.Glide
import org.json.JSONObject

class FillItemBangKaSameChannelDialogFragment : DialogFragment() {

    private var _binding: DiaFillItemBangKaSameChannelBinding? = null
    private val binding get() = _binding!!
    private var _clickListener: (String, HashMap<String, Any>) -> Unit = { _, _ -> }

    private var _dataSource: JSONObject? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val layout = inflater.inflate(R.layout.dia_fill_item_bang_ka_same_channel, container, false)
        _binding = DiaFillItemBangKaSameChannelBinding.bind(layout)
        return layout
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Theme_Bottom_Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        binding.diaFillItemBangKaSameChannelClose.setOnClickListener {
            dismiss()
        }

        binding.diaFillItemBangKaSameChannelNo.setOnClickListener {
            //
            addTouch(type = "bang_ka_same_channel_dialog_no_click")

            dismiss()
        }

        binding.diaFillItemBangKaSameChannelYes.setOnClickListener {
            if (_dataSource != null) {
//                    val logo = item.optString("logo", "")
                val channel = _dataSource!!.optString("walletChannel", "")
                val mobile = _dataSource!!.optString("walletMobile", "")
                val params = hashMapOf<String, Any>().apply {
                    put("walletMobile", mobile)
                    put("walletMobileConfirm", mobile)
                    put("walletChannel", channel)
                    put("addBindFlag", "YES")
                }
                _clickListener.invoke(this.tag.toString(), params)
            }

            //
            addTouch(type = "bang_ka_same_channel_dialog_yes_click")

            dismiss()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()
        val dialog = dialog ?: return
        val window = dialog.window ?: return

        window.attributes.gravity = Gravity.CENTER
        window.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        _dataSource?.let {
            binding.diaFillItemBangKaSameChannelChannel.text = it.optString("walletDesc", "")
            binding.diaFillItemBangKaSameChannelMobile.text = it.optString("walletMobile", "")

            val logo = it.optString("walletLogo", "")
            if (logo.isUrl()) {
                Glide.with(binding.diaFillItemBangKaSameChannelIcon).load(logo)
                    .into(binding.diaFillItemBangKaSameChannelIcon)
            }
        }

        //
        addTouch(type = "bang_ka_same_channel_dialog_show")
    }

    fun addDataSource(value: JSONObject): FillItemBangKaSameChannelDialogFragment {
        try {
            _dataSource = JSONObject(value.toString())
        } catch (e: Exception) {
            //
        }
        return this
    }

    fun addClickListener(listener: (String, HashMap<String, Any>) -> Unit): FillItemBangKaSameChannelDialogFragment {
        _clickListener = listener
        return this
    }
}